package com.mayo.series.user_prreferences;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.mayo.series.comun.ComunUtilidades;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

public class PreferenciasUtils {

	protected static SharedPreferences prefs;
	
	public static final String PREFERENCIAS = "MisPreferencias";
	public static final String ALARMAS = "AlarmasSeries";
	public static final String FAVORITAS  = "SeriesFavoritas";
	public static final String FIRST_TIME_APP = "FirstTimeApp";
	public static final String ID_ALARMA_NUEVOS_CAPITULOS  = "id_alarma_nuevos_capitulos";
	public static final String FIRST_TIME_MIS_SERIES = "FirstTimeMisSeries";
	
	
	private static void getMyPreferences(Context context, String name) {
		prefs = context.getSharedPreferences(name,Context.MODE_PRIVATE);
	}
	
	public static String getStringAtributePreferences(Context context, String name, String atribute, String defValue) {
		getMyPreferences(context, name);
		return prefs.getString(atribute, defValue);
	}
	
	public static void saveStringAtribute(Context context, String name, String atribute, String value) {
		getMyPreferences(context, name);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(atribute, value);
		editor.commit();
	}
	
	public static int getIntAtributePreferences(Context context, String name, String atribute, int defValue) {
		getMyPreferences(context, name);
		return prefs.getInt(atribute, defValue);
	}
	
	public static void saveIntAtribute(Context context, String name, String atribute, int value) {
		getMyPreferences(context, name);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(atribute, value);
		editor.commit();
	}
	
	public static boolean getBooleanAtributePreferences(Context context, String name, String atribute, boolean defValue) {
		getMyPreferences(context, name);
		return prefs.getBoolean(atribute, defValue);
	}
	
	public static void saveBooleanAtribute(Context context, String name, String atribute, boolean value) {
		getMyPreferences(context, name);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(atribute, value);
		editor.commit();
	}
	
	public static void removeStringAtribute(Context context, String name, String atribute) {
		getMyPreferences(context, name);
		SharedPreferences.Editor editor = prefs.edit();
		editor.remove(atribute);
		editor.commit();
	}
	
	public static Set<String> getStringSetAtributePreferences(Context context, String name, String atribute, Set<String> defValue) {
		getMyPreferences(context, name);
		String cadena = prefs.getString(atribute, null);
		String[] array;
		Set<String> returnArray = new HashSet<String>();
		if (!ComunUtilidades.isStringEmpty(cadena)) {
			array = cadena.split("-");
			for (String value : array)
			{
				returnArray.add(value);
			}
		}
		
		return returnArray;
	}
	
	public static void saveStringSetAtribute(Context context, String name, String atribute, Set<String> value) {
		getMyPreferences(context, name);
		SharedPreferences.Editor editor = prefs.edit();
		
		String cadena = ComunUtilidades.getSeriesUsuarioToString(value);
		
		editor.putString(atribute, cadena);
		editor.commit();
	}
}
